//
//  SecondViewController.m
//  UI_豆瓣扩展
//
//  Created by dllo on 15/11/8.
//  Copyright © 2015年 张磊. All rights reserved.
//

#import "SecondViewController.h"
#import "SecondView.h"
#import "DataBaseUser.h"
#import "UserModle.h"
@interface SecondViewController ()

@property (nonatomic, retain)SecondView *secondV;

@end

@implementation SecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationItem.title = @"注册";
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"注册" style:UIBarButtonItemStylePlain target:self action:@selector(rightBar)];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"用户登录" style:UIBarButtonItemStylePlain target:self action:@selector(leftBar)];
    
    self.secondV = [[SecondView alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
    [self.view addSubview:self.secondV];
    [_secondV release];
    
    [[DataBaseUser shareDataBase]openDB];
    [[DataBaseUser shareDataBase]createTable];

    
}
-(void)leftBar
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)rightBar
{
    //根据注册用户名查找数据库,如果返回的数组不为空则说明该用户已经被注册过,不能再注册,若不为空就可以注册
    NSMutableArray *array = [[DataBaseUser shareDataBase]selectUser:self.secondV.nameTextFild.text];
    if (array.count != 0) {
        
        UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"" message:@"该用户已经被注册" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *alertA = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];
        UIAlertAction *alertA1 = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:nil];
        [alertC addAction:alertA];
        [alertC addAction:alertA1];
        [self presentViewController:alertC animated:YES completion:nil];
        
    }else if([self.secondV.nameTextFild.text isEqualToString:@""] || [self.secondV.passwordTextFild.text isEqualToString:@""]){
        
        UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"未添加用户名或密码" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *alertA = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];
        [alertC addAction:alertA];
        [self presentViewController:alertC animated:YES completion:nil];
    }else{
        UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"" message:@"注册成功" preferredStyle:UIAlertControllerStyleAlert];
        [self presentViewController:alertC animated:YES completion:nil];
        [self dismissViewControllerAnimated:YES completion:^{
            
            UserModle *user = [[UserModle alloc]init];
            user.userName = self.secondV.nameTextFild.text;
            user.password = self.secondV.passwordTextFild.text;
            [[DataBaseUser shareDataBase]insertStudent:user];
            
            [self dismissViewControllerAnimated:YES completion:nil];
        }];
        
        
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
